package com.syaccp.controller;


import com.github.pagehelper.PageInfo;
import com.syaccp.model.SysDictType;
import com.syaccp.request.DictTypeDTO;
import com.syaccp.service.DictTypeService;
import com.syaccp.utils.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author lixin
 * @since 2023-07-02
 */
@RestController
@RequestMapping("/api/dictType")
public class DictTypeController {


    @Resource
    private DictTypeService dictTypeService;

    /**
     * 分页查询
     * @param dictTypeDTO
     * @return
     */
    @ApiOperation(value = "字典分页角色",notes = "字典信息")
    @GetMapping(value = "/list")
    public Result<PageInfo<SysDictType>> list(@ApiParam(value = "查询的条件") DictTypeDTO dictTypeDTO){
        PageInfo<SysDictType> sysDictTypePageInfo = dictTypeService.queryPage(dictTypeDTO);

        return Result.success(sysDictTypePageInfo);
    }

    /**
     * 新增
     * @param sysDictType
     * @return
     */
    @ApiOperation(value = "新增字典信息",notes = "新增字典信息")
    @PostMapping(value = "/save")
    public Result<SysDictType> saveAndUpdate(@RequestBody @Validated SysDictType sysDictType){
        dictTypeService.saveAndUpdate(sysDictType);
        return Result.success();
    }

    /**
     * 根据字典id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询字典信息",notes = "根据id查询字典信息")
    @GetMapping(value = "/get/{id}")
    public Result<SysDictType> get(@PathVariable Integer id){
        SysDictType byId = dictTypeService.getById(id);
        return Result.success(byId);
    }

    /**
     * 修改
     * @param sysDictType
     * @return
     */
    @ApiOperation(value = "修改字典信息",notes = "修改字典信息")
    @PostMapping(value = "/update")
    public Result<SysDictType> update(@RequestBody @Validated SysDictType sysDictType){
        if (dictTypeService.updateById(sysDictType)) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除字典信息",notes = "根据id删除字典信息")
    @DeleteMapping(value = "/delete/{id}")
    public Result<SysDictType> delete(@PathVariable Integer id){
        if (dictTypeService.removeById(id)) {
            return Result.success();
        }
        return Result.fail();
    }


}

